<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var lengthOfLIS = function (nums) {
        if (!nums.length) return []
        let result = [[nums[0]]]
        //创建二维数组 result，然后从1遍历nums
        for (let i = 1; i < nums.length; i++) {
          const n = nums[i]
          _update(n)
        }
        return result[result.length - 1]
        function _update(n) {
          //从后往前遍历result数组，获得一维数组，取出末尾数字
          for (let i = result.length - 1; i >= 0; i--) {
            let line = result[i]
            let tail = line[line.length - 1]
            //因为是从后往前遍历，如果当前n大于了末尾，那么就添加一行呗
            if (n > tail) {
              result[i + 1] = [...line, n]
              break
            } else if (n < tail && i == 0) {
              result[i] = [n]
            }
          }
        }
      }
      console.log(lengthOfLIS([10, 9, 2, 5, 3, 7, 101, 18]))
    </script>
  </body>
</html>
